<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class HYD_Model extends CI_Model {

    /**
     * 主表名称 
     *
     * @var string
     */
    private $_table_name = '';

    /**
     * Constructor
     *
     * @access  public
     */
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function set_table_name($table_name) {
        $this->_table_name = $table_name;
    }

    public function get_table_name() {
        return $this->_table_name;
    }

    /**
     * 获取指定表所有字段列表
     *
     * 获取指定表所有字段列表
     * @param	 string $table_name 表名称
     * @return	string $select_list 字段列表串
     */
    public function get_all_feilds($table_name = '') {
        if (empty($table_name)) {
           $table_name = $this->_table_name;
        }

        $fields = $this->db->field_data($table_name);
        $select_list = '';

        foreach ($fields as $field) {
            $select_list .= $field->name . ',';
        }
        $select_list = chop($select_list, ',');
        return $select_list;
    }

    /**
     * 获取需要更新的字段列表
     *
     * 获取需要更新的字段列表，根据传入的data数组进行比对，返回可更新的字段列表
     * @param	 array $data 前台提交的数据
     * @return	array 用于更新数据的数组
     */
    public function get_update_feilds($data = array(), $table_name = '') {
        if (empty($table_name))
            $table_name = $this->_table_name;
        $fields = $this->db->field_data($table_name);
        $row_data = array();

        foreach ($fields as $field) {
            if ($field->primary_key)
                continue;
            if (in_array($field->name, array_keys($data))) {
                $row_data[$field->name] = $data[$field->name];
            }
        }
        return $row_data;
    }
    /**
     * 检查数据是否重复
     * @params type 数组
     * @return boolean
     */
    public function get_is_duplicate($parms = array()) {
        if (is_array($parms['where']) && !empty($parms['where'])) {

            if (!isset($parms['table_name'])) {
                $parms['table_name'] = $this->_table_name;
            }

            if (!isset($parms['id_feild'])) {
                $parms['id_feild'] = 'id ';
            }

            $this->db
                    ->select($parms['id_feild'])
                    ->from($parms['table_name'])
                    ->where($parms['where']);

            if (is_numeric($parms['id_value'])) {
                $this->db->where("{$parms['id_feild']}!=", $parms['id_value']);
            }
            $check_data = $this->db->get();
            if ($check_data->num_rows() > 0) {
                return TRUE;
            }
        }
        return FALSE;
    }

    /**
     * 检查事务并记录数据库错误日志
     *
     * 检查事务并记录数据库错误日志 ，示例" if( $this->check_dbtrans() === FALSE) return MODEL_RETURN_DBERROR;
     * @param	 bool $endapp 发生错误时是否终止程序
     * @return	bool TRUE=无错误 FALSE=有错误
     */
    public function check_dbtrans($endapp = FALSE) {
        if ($this->db->trans_status() === FALSE) {
            $errinfo = array();
            $errinfo['filename'] = __FILE__;
            $errinfo['function'] = __FUNCTION__;
            $errinfo['linenumber'] = __LINE__;
            $errinfo['dberror'] = $this->db->error();

            $this->db->trans_rollback();

            $db_Exception = & load_class('DBException', 'core');
            $db_Exception->log_dberror($errinfo, $endapp);
            return FALSE;
        }
        return TRUE;
    }

    /**
     * 获取指定条件记录数
     *
     * 获取指定条件记录数 
     * @param	 string $table_name 表名
     * @param	 array $where 查询条件数组
     * @return	integer 记录数
     */
    public function check_records_count($table_name, $where = array()) {

        if (count($where) > 0) {

            $this->db->from($table_name);
            $this->db->where($where);

            return $this->db->count_all_results();
        } else {
            return $this->db->count_all($table_name);
        }
    }

    /**
     * build where string
     *
     * 组建where条件
     *
     * @access	public
     * @param	string $sql 现有sql串
     * @param	string $feild 字段
     * @param	string $value  值
     * @param	string $sqlflag 逻辑操作符
     * @param	string $opr 运算符
     * @return	string
     */
    function sql_build_where($sql = '', $feild = '', $value = '', $sqlflag = 'and', $opr = '=') {

        if (!isset($feild) || strlen($feild) == 0 || strlen($value) == 0) {
            return $sql;
        } else {
            $value = $this->db->escape($value);
            if (strlen(trim($sql)) == 0) {
                $sql = " {$feild} $opr {$value} ";
            } else {
                $sql.=" {$sqlflag} {$feild} $opr {$value} ";
            }
        }
        return $sql;
    }

}